
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>单周期CPU实现 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="2五级流水.html" />
    
    
    <link rel="prev" href="../../gem5/custom_disk_img.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">BoomV3</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../BoomCode/chipyard-install.html">
            
                <a href="../../BoomCode/chipyard-install.html">
            
                    
                    Chipyard 1.8.0安装教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../BoomCode/boom-debug.html">
            
                <a href="../../BoomCode/boom-debug.html">
            
                    
                    Boom调试【更新中】
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">论文阅读</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../paper_read/prefetch-guard.html">
            
                <a href="../../paper_read/prefetch-guard.html">
            
                    
                    Prefetch-guard
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Gem5 学习</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../gem5/gem5_fs.html">
            
                <a href="../../gem5/gem5_fs.html">
            
                    
                    X86 全系统模式模拟
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../gem5/custom_disk_img.html">
            
                <a href="../../gem5/custom_disk_img.html">
            
                    
                    自定义磁盘镜像
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">一生一芯FangCore实现</li>
        
        
    
        <li class="chapter active" data-level="5.1" data-path="1单周期CPU.html">
            
                <a href="1单周期CPU.html">
            
                    
                    单周期CPU实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="2五级流水.html">
            
                <a href="2五级流水.html">
            
                    
                    顺序五级流水CPU设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="3乘除法器.html">
            
                <a href="3乘除法器.html">
            
                    
                    乘除法功能单元
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="4AXI4总线.html">
            
                <a href="4AXI4总线.html">
            
                    
                    AXI4总线
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="5高速缓存Cache.html">
            
                <a href="5高速缓存Cache.html">
            
                    
                    高速缓存Cache设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="6中断异常.html">
            
                <a href="6中断异常.html">
            
                    
                    简单的中断异常支持
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="7分支预测器.html">
            
                <a href="7分支预测器.html">
            
                    
                    分支预测器
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >单周期CPU实现</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x5355;&#x5468;&#x671F;cpu&#x8BBE;&#x8BA1;">&#x5355;&#x5468;&#x671F;CPU&#x8BBE;&#x8BA1;</h2>
<h2 id="&#x53C2;&#x8003;&#x5185;&#x5BB9;">&#x53C2;&#x8003;&#x5185;&#x5BB9;</h2>
<blockquote>
<ul>
<li>The RISC-V Instruction Set Manual (<a href="https://github.com/riscv/riscv-isa-manual/releases/download/draft-20210813-7d0006e/riscv-spec.pdf" target="_blank">Volume I</a>&#xFF09;</li>
<li>&#x300A;CPU&#x8BBE;&#x8BA1;&#x5B9E;&#x6218;&#x300B;&#x7B2C;&#x56DB;&#x7AE0;P75-P92</li>
<li>&#x300A;&#x8BA1;&#x7B97;&#x673A;&#x7EC4;&#x6210;&#x4E0E;&#x8BBE;&#x8BA1; &#x786C;&#x4EF6;&#x8F6F;&#x4EF6;&#x63A5;&#x53E3;&#xFF08;&#x7B2C;&#x4E94;&#x7248;&#xFF09;&#x300B;&#x7B2C;&#x56DB;&#x7AE0;4.1-4.4&#x5C0F;&#x8282;</li>
<li><a href="https://github.com/OSCPU/NutShell" target="_blank">NutShell&#x6E90;&#x7801;</a></li>
<li><a href="https://github.com/yuhanzhu612/Trigger" target="_blank">Trigger&#xFF1A;chisel&#x5199;&#x7684;4&#x7EA7;&#x6D41;&#x6C34;&#x7EBF;&#x987A;&#x5E8F;&#x5355;&#x53D1;&#x5C04;&#x5904;&#x7406;&#x5668;&#x6838;</a></li>
</ul>
</blockquote>
<p>&#x5728;&#x6B21;&#x4E4B;&#x524D;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x5BF9;RISC-V&#x6307;&#x4EE4;&#x96C6;&#x6709;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4E86;&#x89E3;&#x3002;&#x672C;&#x6587;&#x7684;&#x5355;&#x5468;&#x671F;&#x5904;&#x7406;&#x5668;&#x652F;&#x6301;<strong>RISCV64IM</strong>&#x6307;&#x4EE4;&#x3002;</p>
<p><img src="https://cdn.jsdelivr.net/gh/JiangFang03/images/img/202207222239163.png" alt="image-20220722223959062"></p>
<h2 id="1&#x3001;&#x8BD1;&#x7801;&#x6A21;&#x5757;">1&#x3001;&#x8BD1;&#x7801;&#x6A21;&#x5757;</h2>
<h3 id="&#x6307;&#x4EE4;&#x96C6;">&#x6307;&#x4EE4;&#x96C6;</h3>
<p>&#x8FD9;&#x91CC;&#x4EFF;&#x7167;&#x679C;&#x58F3;Nutshell&#x7684;&#x8BD1;&#x7801;&#x65B9;&#x5F0F;&#x3002;&#x901A;&#x8FC7;BitPat&#x5339;&#x914D;&#x6307;&#x4EE4;&#xFF0C;&#x540C;&#x65F6;&#x6BCF;&#x4E00;&#x6761;&#x6307;&#x4EE4;&#x90FD;&#x7ED1;&#x5B9A;&#x4E86;&#x5177;&#x4F53;&#x7684;&#x6307;&#x4EE4;&#x7C7B;&#x578B;&#x3001;&#x64CD;&#x4F5C;&#x5355;&#x5143;&#x4EE5;&#x53CA;&#x5177;&#x4F53;&#x7684;&#x64CD;&#x4F5C;&#x3002;</p>
<pre><code class="lang-scala">  <span class="hljs-comment">//RV64MInstr</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">MULW</span>    </span>= <span class="hljs-type">BitPat</span>(<span class="hljs-string">&quot;b0000001_?????_?????_000_?????_0111011&quot;</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">DIVW</span>    </span>= <span class="hljs-type">BitPat</span>(<span class="hljs-string">&quot;b0000001_?????_?????_100_?????_0111011&quot;</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">DIVUW</span>   </span>= <span class="hljs-type">BitPat</span>(<span class="hljs-string">&quot;b0000001_?????_?????_101_?????_0111011&quot;</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">REMW</span>    </span>= <span class="hljs-type">BitPat</span>(<span class="hljs-string">&quot;b0000001_?????_?????_110_?????_0111011&quot;</span>)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">REMUW</span>   </span>= <span class="hljs-type">BitPat</span>(<span class="hljs-string">&quot;b0000001_?????_?????_111_?????_0111011&quot;</span>)

  <span class="hljs-keyword">val</span> <span class="hljs-type">RV64M_Table</span> = <span class="hljs-type">Array</span>(
    <span class="hljs-type">MULW</span>           -&gt; <span class="hljs-type">List</span>(<span class="hljs-type">InstrR</span>, <span class="hljs-type">FuType</span>.mdu, <span class="hljs-type">MDUOpType</span>.mulw),
    <span class="hljs-type">DIVW</span>           -&gt; <span class="hljs-type">List</span>(<span class="hljs-type">InstrR</span>, <span class="hljs-type">FuType</span>.mdu, <span class="hljs-type">MDUOpType</span>.divw),
    <span class="hljs-type">DIVUW</span>          -&gt; <span class="hljs-type">List</span>(<span class="hljs-type">InstrR</span>, <span class="hljs-type">FuType</span>.mdu, <span class="hljs-type">MDUOpType</span>.divuw),
    <span class="hljs-type">REMW</span>           -&gt; <span class="hljs-type">List</span>(<span class="hljs-type">InstrR</span>, <span class="hljs-type">FuType</span>.mdu, <span class="hljs-type">MDUOpType</span>.remw),
    <span class="hljs-type">REMUW</span>          -&gt; <span class="hljs-type">List</span>(<span class="hljs-type">InstrR</span>, <span class="hljs-type">FuType</span>.mdu, <span class="hljs-type">MDUOpType</span>.remuw)
  )
</code></pre>
<p>&#x5728;&#x8BD1;&#x7801;&#x5355;&#x5143;&#xFF0C;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x5C31;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x6307;&#x4EE4;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x64CD;&#x4F5C;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-scala">    <span class="hljs-keyword">val</span> decodeList = <span class="hljs-type">ListLookup</span>(instr, <span class="hljs-type">Instructions</span>.<span class="hljs-type">DecodeDefault</span>, <span class="hljs-type">Instructions</span>.<span class="hljs-type">DecodeTable</span>)
    <span class="hljs-keyword">val</span> instrType = decodeList(<span class="hljs-number">0</span>) 
    <span class="hljs-keyword">val</span> fuType  = decodeList(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">val</span> fuOpType = decodeList(<span class="hljs-number">2</span>)
</code></pre>
<h3 id="&#x63A7;&#x5236;&#x4FE1;&#x53F7;">&#x63A7;&#x5236;&#x4FE1;&#x53F7;</h3>
<p>&#x8BD1;&#x7801;&#x6A21;&#x5757;&#x6839;&#x636E;<code>instrType</code>&#x3001;<code>fuType</code>&#x3001;<code>fuOpType</code>&#x751F;&#x6210;&#x4EE5;&#x4E0B;&#x63A7;&#x5236;&#x4FE1;&#x53F7;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x63A7;&#x5236;&#x4FE1;&#x53F7;</th>
<th>&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td>instrType</td>
<td>&#x6307;&#x4EE4;&#x7C7B;&#x578B;&#xFF08;I&#x3001;R&#x3001;S&#x3001;B&#x3001;U&#x3001;J&#xFF09;</td>
</tr>
<tr>
<td>fuType</td>
<td>&#x64CD;&#x4F5C;&#x5355;&#x5143;&#xFF08;ALU&#x3001;LSU&#x3001;MDU&#x7B49;&#xFF09;</td>
</tr>
<tr>
<td>fuOpType</td>
<td>&#x5177;&#x4F53;&#x64CD;&#x4F5C;&#xFF08;&#x5982;add&#x3001;sub&#x3001;jal&#x7B49;&#xFF09;</td>
</tr>
<tr>
<td>alu1Type</td>
<td>ALU&#x64CD;&#x4F5C;&#x6570;1&#x7C7B;&#x578B;&#xFF0C;0&#xFF1A;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;1&#xFF1A;PC</td>
</tr>
<tr>
<td>alu2Type</td>
<td>ALU&#x64CD;&#x4F5C;&#x6570;2&#x7C7B;&#x578B;&#xFF0C;0&#xFF1A;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;1&#xFF1A;imm</td>
</tr>
<tr>
<td>rfSrc1</td>
<td>&#x6E90;&#x5BC4;&#x5B58;&#x5668;rs</td>
</tr>
<tr>
<td>rfSrc2</td>
<td>&#x6E90;&#x5BC4;&#x5B58;&#x5668;rt</td>
</tr>
<tr>
<td>rfWen</td>
<td>&#x5BC4;&#x5B58;&#x5668;&#x5806;&#x5199;&#x4F7F;&#x80FD;</td>
</tr>
<tr>
<td>rfDest</td>
<td>&#x76EE;&#x7684;&#x5BC4;&#x5B58;&#x5668;</td>
</tr>
<tr>
<td>imm</td>
<td>&#x7ACB;&#x5373;&#x6570;</td>
</tr>
<tr>
<td>sel_rf_res</td>
<td>&#x5199;&#x56DE;&#x5BC4;&#x5B58;&#x5668;&#x5806;&#x7684;&#x6765;&#x6E90;&#x3002;0&#xFF1A;ALU&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#xFF0C;1&#xFF1A;&#x5B58;&#x50A8;&#x5668;&#x6570;&#x636E;</td>
</tr>
<tr>
<td>mem_write</td>
<td>&#x5B58;&#x50A8;&#x5668;&#x5199;&#x4F7F;&#x80FD;</td>
</tr>
<tr>
<td>mem_read</td>
<td>&#x5B58;&#x50A8;&#x5668;&#x8BFB;&#x4F7F;&#x80FD;</td>
</tr>
</tbody>
</table>
<h2 id="2&#x3001;alu&#x5355;&#x5143;">2&#x3001;ALU&#x5355;&#x5143;</h2>
<p>ALU&#x5355;&#x5143;&#x6839;&#x636E;&#x4F20;&#x5165;&#x7684;<code>aluopType</code>&#x8FDB;&#x884C;&#x4E0D;&#x540C;&#x7684;&#x8BA1;&#x7B97;&#x3002;&#x540C;&#x65F6;&#x8D1F;&#x8D23;&#x8BA1;&#x7B97;next_Pc&#x548C;&#x8BBF;&#x5B58;&#x7684;&#x5730;&#x5740;</p>
<pre><code class="lang-scala">    <span class="hljs-keyword">val</span> res = <span class="hljs-type">LookupTreeDefault</span>(aluOpType, adderRes, <span class="hljs-type">List</span>(
        <span class="hljs-type">ALUOpType</span>.sll   -&gt; ((src1  &lt;&lt; shamt)(<span class="hljs-type">XLEN</span><span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)), <span class="hljs-comment">//&#x903B;&#x8F91;&#x5DE6;&#x79FB;</span>
        <span class="hljs-type">ALUOpType</span>.srl   -&gt; (src1  &gt;&gt; shamt),
        <span class="hljs-type">ALUOpType</span>.sra   -&gt; ((src1.asSInt &gt;&gt; shamt).asUInt),

        <span class="hljs-type">ALUOpType</span>.slt   -&gt; <span class="hljs-type">ZeroExt</span>(less, <span class="hljs-type">XLEN</span>),
        <span class="hljs-type">ALUOpType</span>.sltu  -&gt; <span class="hljs-type">ZeroExt</span>(less_u, <span class="hljs-type">XLEN</span>),

        <span class="hljs-type">ALUOpType</span>.xor   -&gt; (src1 ^ src2),
        <span class="hljs-type">ALUOpType</span>.or    -&gt; (src1 | src2),
        <span class="hljs-type">ALUOpType</span>.and   -&gt; (src1 &amp; src2),

        <span class="hljs-type">ALUOpType</span>.addw  -&gt; <span class="hljs-type">SignExt</span>((adderRes)(<span class="hljs-number">31</span>,<span class="hljs-number">0</span>), <span class="hljs-number">64</span>),
        <span class="hljs-type">ALUOpType</span>.subw  -&gt; <span class="hljs-type">SignExt</span>((adderRes)(<span class="hljs-number">31</span>,<span class="hljs-number">0</span>), <span class="hljs-number">64</span>),
        <span class="hljs-type">ALUOpType</span>.sllw  -&gt; <span class="hljs-type">SignExt</span>((src1  &lt;&lt; shamt)(<span class="hljs-number">31</span>, <span class="hljs-number">0</span>),<span class="hljs-number">64</span>),
        <span class="hljs-type">ALUOpType</span>.srlw  -&gt; <span class="hljs-type">SignExt</span>((shsrc1  &gt;&gt; shamt)(<span class="hljs-number">31</span>,<span class="hljs-number">0</span>),<span class="hljs-number">64</span>),
        <span class="hljs-type">ALUOpType</span>.sraw  -&gt; <span class="hljs-type">SignExt</span>(((shsrc1.asSInt &gt;&gt; shamt).asUInt)(<span class="hljs-number">31</span>,<span class="hljs-number">0</span>) ,<span class="hljs-number">64</span>),

        <span class="hljs-type">ALUOpType</span>.beq   -&gt; !(src1 ^ src2).orR,
        <span class="hljs-type">ALUOpType</span>.bne   -&gt; (src1 ^ src2).orR,
        <span class="hljs-type">ALUOpType</span>.blt   -&gt; less,
        <span class="hljs-type">ALUOpType</span>.bltu  -&gt; less_u,
        <span class="hljs-type">ALUOpType</span>.bge   -&gt; !less,
        <span class="hljs-type">ALUOpType</span>.bgeu  -&gt; !less_u,
    ))
</code></pre>
<h3 id="&#x7279;&#x6B8A;&#x6307;&#x4EE4;&#x7684;&#x5904;&#x7406;">&#x7279;&#x6B8A;&#x6307;&#x4EE4;&#x7684;&#x5904;&#x7406;</h3>
<p>&#x9700;&#x8981;&#x7279;&#x522B;&#x5173;&#x6CE8;&#x7684;&#x6307;&#x4EE4;&#xFF1A;</p>
<h4 id="lui&#xFF1A;"><strong><code>LUI&#xFF1A;</code></strong></h4>
<p>&#x52A0;&#x8F7D;&#x957F;&#x7ACB;&#x5373;&#x6570;&#x5230;rd&#x4E2D;&#x3002;&#x64CD;&#x4F5C;&#x6570;1&#xFF1A;0&#x53F7;&#x5BC4;&#x5B58;&#x5668;&#x3002;&#x64CD;&#x4F5C;&#x6570;2&#xFF1A;&#x7ACB;&#x5373;&#x6570;&#x3002;&#x96F6;&#x53F7;&#x5BC4;&#x5B58;&#x5668;&#x52A0;&#x4E0A;&#x7ACB;&#x5373;&#x6570;&#x5199;&#x5165;rd&#x5BC4;&#x5B58;&#x5668;</p>
<p>&#x1F4A1; ALU&#x52A0;&#x6CD5;&#x64CD;&#x4F5C;&#xFF1A;rd = reg(0)+imm</p>
<h4 id="auipc&#xFF1A;"><strong><code>AUIPC&#xFF1A;</code></strong></h4>
<p>PC&#x52A0;&#x4E0A;&#x7ACB;&#x5373;&#x6570;&#x3002;&#x64CD;&#x4F5C;&#x6570;1&#xFF1A;pc&#xFF1B;&#x64CD;&#x4F5C;&#x6570;2&#xFF1A;&#x7ACB;&#x5373;&#x6570;</p>
<p> &#x1F4A1; ALU&#x52A0;&#x6CD5;&#x64CD;&#x4F5C;&#xFF1A;rd = PC+imm</p>
<h4 id="jal&#xFF1A;&#x65E0;&#x6761;&#x4EF6;&#x8DF3;&#x8F6C;"><code>JAL&#xFF1A;</code>&#x65E0;&#x6761;&#x4EF6;&#x8DF3;&#x8F6C;</h4>
<p>&#xFF08;JAL&#x3001;JALR&#x53EF;&#x4EE5;&#x5728;&#x8BD1;&#x7801;&#x9636;&#x6BB5;&#x5C31;&#x80FD;&#x83B7;&#x5F97;&#x8DF3;&#x8F6C;&#x5730;&#x5740;&#x3002;&#xFF09;&#x64CD;&#x4F5C;&#x6570;1&#xFF1A;pc&#xFF1B;&#x64CD;&#x4F5C;&#x6570;2&#xFF1A;&#x7ACB;&#x5373;&#x6570;(offset)</p>
<p>&#x8BB0;&#x5F55;&#x4E0B;&#x4E00;&#x6761;pc&#x503C;(rd = pc+4), pc+4&#x5E94;&#x8BE5;&#x4F5C;&#x4E3A;ALU&#x7684;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#xFF0C;&#x6700;&#x7EC8;&#x5199;&#x56DE;&#x5BC4;&#x5B58;&#x5668;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x8BA1;&#x7B97;&#x76EE;&#x6807;&#x5730;&#x5740;&#xFF08;nextPc)&#x8BBE;&#x4E3A; pc + offset&#xFF08;imm)</p>
<p>&#x1F4A1; ALU&#x52A0;&#x6CD5;&#x64CD;&#x4F5C;&#xFF1A;res = pc + 4;  target = pc+offset</p>
<h4 id="jalr&#xFF1A;&#x94FE;&#x63A5;&#x5E76;&#x8DF3;&#x8F6C;"><strong><code>JALR&#xFF1A;</code></strong>&#x94FE;&#x63A5;&#x5E76;&#x8DF3;&#x8F6C;</h4>
<p>&#x64CD;&#x4F5C;&#x6570;1&#xFF1A;rs1&#xFF08;&#x5BC4;&#x5B58;&#x5668;&#x8BFB;&#x7AEF;&#x53E3;1&#x7684;&#x503C;&#xFF09;&#x64CD;&#x4F5C;&#x6570;2&#xFF1A;&#x7ACB;&#x5373;&#x6570;&#xFF08;offset)&#x3002;</p>
<p>&#x8BB0;&#x5F55;&#x4E0B;&#x4E00;&#x6761;pc&#x503C;(rd = pc+4), pc+4&#x5E94;&#x8BE5;&#x4F5C;&#x4E3A;ALU&#x7684;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#xFF0C;&#x6700;&#x7EC8;&#x5199;&#x56DE;&#x5BC4;&#x5B58;&#x5668;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x8BA1;&#x7B97;&#x76EE;&#x6807;&#x5730;&#x5740;&#xFF08;nextPc)&#x8BBE;&#x4E3A; reg + offset&#xFF08;imm)</p>
<p> &#x1F4A1; ALU&#x52A0;&#x6CD5;&#x64CD;&#x4F5C;&#xFF1A;res = pc + 4;  target = reg+offset</p>
<h4 id="b&#x7C7B;&#x6307;&#x4EE4;"><strong>B&#x7C7B;</strong>&#x6307;&#x4EE4;</h4>
<p>&#x64CD;&#x4F5C;&#x6570;1&#xFF1A;rs1&#xFF08;&#x5BC4;&#x5B58;&#x5668;&#x8BFB;&#x7AEF;&#x53E3;1&#x7684;&#x503C;&#xFF09;&#x64CD;&#x4F5C;&#x6570;2&#xFF1A;rs2&#xFF08;&#x5BC4;&#x5B58;&#x5668;&#x8BFB;&#x7AEF;&#x53E3;1&#x7684;&#x503C;&#xFF09;</p>
<p>ALU&#x6BD4;&#x8F83;rs1&#x548C;rs2&#xFF08;&#x76F8;&#x7B49;&#x3001;&#x5C0F;&#x4E8E;&#x3001;&#x5927;&#x4E8E;&#x2026;&#x2026;&#xFF09;&#x3002;ALU&#x5F97;&#x51FA;&#x662F;&#x5426;&#x8DF3;&#x8F6C;&#xFF1A;taken&#x3002;&#x5982;&#x679C;taken&#xFF0C;</p>
<p>&#x8BBE;&#x7F6E;target_pc = pc + offset</p>
<h2 id="3&#x3001;mdu&#x4E58;&#x9664;&#x6CD5;&#x5355;&#x5143;">3&#x3001;MDU&#x4E58;&#x9664;&#x6CD5;&#x5355;&#x5143;</h2>
<p>&#x4E58;&#x9664;&#x6CD5;&#x8BA1;&#x7B97;&#x3002;&#x76EE;&#x524D;&#x76F4;&#x63A5;&#x7528;<code>*</code>&#x3001;<code>/</code>&#x548C;<code>%</code></p>
<pre><code class="lang-scala">    <span class="hljs-keyword">val</span> mulRes = src1 * src2
    <span class="hljs-keyword">val</span> res = <span class="hljs-type">LookupTreeDefault</span>(mduOpType, mulRes, <span class="hljs-type">List</span>(
        <span class="hljs-type">MDUOpType</span>.mul       -&gt; (src1 * src2)(<span class="hljs-number">63</span>, <span class="hljs-number">0</span>),
        <span class="hljs-type">MDUOpType</span>.mulh      -&gt; ((src1.asSInt * src2.asSInt).asUInt &gt;&gt; <span class="hljs-number">32</span>),

        <span class="hljs-type">MDUOpType</span>.div        -&gt;  (src1.asSInt / src2.asSInt).asUInt,
        <span class="hljs-type">MDUOpType</span>.divu       -&gt; (src1 / src2)(<span class="hljs-number">63</span>,<span class="hljs-number">0</span>),

        <span class="hljs-type">MDUOpType</span>.rem        -&gt; (src1.asSInt % src2.asSInt).asUInt,
        <span class="hljs-type">MDUOpType</span>.remu       -&gt; (src1 % src2),
        &#x2026;&#x2026;
    ))
</code></pre>
<h2 id="4&#x3001;&#x5B58;&#x50A8;&#x6A21;&#x5757;">4&#x3001;&#x5B58;&#x50A8;&#x6A21;&#x5757;</h2>
<p>&#x2460;&#x4F7F;&#x7528;C&#x8BED;&#x8A00;&#x7533;&#x8BF7;&#x4E00;&#x4E2A;&#x53EF;&#x8BFB;&#x5199;&#x7684;&#x5927;&#x6570;&#x7EC4;&#xFF0C;&#x6A21;&#x62DF;&#x5185;&#x5B58;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-keyword">uint8_t</span> pmem[CONFIG_MSIZE];
<span class="hljs-keyword">extern</span> <span class="hljs-string">&quot;C&quot;</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pmem_read</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> raddr, <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> *rdata)</span> </span>{
  <span class="hljs-comment">// &#x603B;&#x662F;&#x8BFB;&#x53D6;&#x5730;&#x5740;&#x4E3A;`raddr &amp; ~0x7ull`&#x7684;8&#x5B57;&#x8282;&#x8FD4;&#x56DE;&#x7ED9;`rdata`</span>
}
<span class="hljs-keyword">extern</span> <span class="hljs-string">&quot;C&quot;</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pmem_write</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> waddr, <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> wdata, <span class="hljs-keyword">char</span> wmask)</span> </span>{
  <span class="hljs-comment">// &#x603B;&#x662F;&#x5F80;&#x5730;&#x5740;&#x4E3A;`waddr &amp; ~0x7ull`&#x7684;8&#x5B57;&#x8282;&#x6309;&#x5199;&#x63A9;&#x7801;`wmask`&#x5199;&#x5165;`wdata`</span>
  <span class="hljs-comment">// `wmask`&#x4E2D;&#x6BCF;&#x6BD4;&#x7279;&#x8868;&#x793A;`wdata`&#x4E2D;1&#x4E2A;&#x5B57;&#x8282;&#x7684;&#x63A9;&#x7801;,</span>
  <span class="hljs-comment">// &#x5982;`wmask = 0x3`&#x4EE3;&#x8868;&#x53EA;&#x5199;&#x5165;&#x6700;&#x4F4E;2&#x4E2A;&#x5B57;&#x8282;, &#x5185;&#x5B58;&#x4E2D;&#x7684;&#x5176;&#x5B83;&#x5B57;&#x8282;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;</span>
}
</code></pre>
<p>&#x2461;&#x7528;verilog&#x5199;&#x5B58;&#x50A8;&#x5668;&#x90E8;&#x5206;&#xFF0C;&#x4F7F;&#x7528;DPI-C&#x673A;&#x5236;&#xFF0C;&#x8C03;&#x7528;&#x8BFB;&#x5199;&#x5B58;&#x50A8;&#x5668;&#x51FD;&#x6570;</p>
<pre><code class="lang-verilog">import <span class="hljs-string">&quot;DPI-C&quot;</span> <span class="hljs-keyword">function</span> void pmem_read(
  <span class="hljs-keyword">input</span> longint raddr, <span class="hljs-keyword">output</span> longint rdata);
import <span class="hljs-string">&quot;DPI-C&quot;</span> <span class="hljs-keyword">function</span> void pmem_write(
  <span class="hljs-keyword">input</span> longint waddr, <span class="hljs-keyword">input</span> longint wdata, <span class="hljs-keyword">input</span> byte wmask);

<span class="hljs-number">XXXX</span>
<span class="hljs-keyword">always</span> @(*) <span class="hljs-keyword">begin</span>
      <span class="hljs-keyword">if</span>(mem_read == <span class="hljs-number">1&apos;b1</span>)
      <span class="hljs-keyword">begin</span>
         pmem_read(address, read_data);
      <span class="hljs-keyword">end</span>
      <span class="hljs-keyword">if</span>(mem_write == <span class="hljs-number">1&apos;b1</span>) 
      <span class="hljs-keyword">begin</span>
         pmem_write(address, write_data, w_mask);
      <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x2462;&#x4F7F;&#x7528;chisel&#x7684;blackbox&#x673A;&#x5236;&#x5305;&#x88F9;PmemHarness.v</p>
<pre><code class="lang-verilog">class PmemHarness extends BlackBox with HasBlackBoxResource {
    val io = IO(new Bundle{
        val clock = Input(Clock())
        val reset = Input(Bool())
        val mem_read = Input(UInt(<span class="hljs-number">1</span>.W))<span class="hljs-comment">//control signal</span>
        val mem_write = Input(UInt(<span class="hljs-number">1</span>.W))<span class="hljs-comment">//control signal</span>
         &#x2026;&#x2026;
    })
  addResource(<span class="hljs-string">&quot;/vsrc/PmemHarness.v&quot;</span>)
}
</code></pre>
<p>pmem_read&#x548C;pmem_write&#x4E2D;&#x6A21;&#x62DF;&#x4E86;64&#x4F4D;&#x603B;&#x7EBF;&#x7684;&#x884C;&#x4E3A;: &#x5B83;&#x4EEC;&#x53EA;&#x652F;&#x6301;&#x5730;&#x5740;&#x6309;8&#x5B57;&#x8282;&#x5BF9;&#x9F50;&#x7684;&#x8BFB;&#x5199;, &#x5176;&#x4E2D;&#x8BFB;&#x64CD;&#x4F5C;&#x603B;&#x662F;&#x8FD4;&#x56DE;&#x6309;8&#x5B57;&#x8282;&#x5BF9;&#x9F50;&#x8BFB;&#x51FA;&#x7684;&#x6570;&#x636E;, &#x9700;&#x8981;&#x7531;RTL&#x4EE3;&#x7801;&#x6839;&#x636E;&#x8BFB;&#x5730;&#x5740;&#x9009;&#x62E9;&#x51FA;&#x9700;&#x8981;&#x7684;&#x90E8;&#x5206;:</p>
<pre><code>val mask = LookupTreeDefault(lsuOpType, &quot;b1111_1111&quot;.U, List(
      LSUOpType.sb -&gt; &quot;b0000_0001&quot;.U,
      LSUOpType.sh -&gt; &quot;b0000_0011&quot;.U,
      LSUOpType.sw -&gt; &quot;b0000_1111&quot;.U,
      LSUOpType.sd -&gt; &quot;b1111_1111&quot;.U
  ))
  mem.io.w_mask := mask

  val rdataSel = mem.io.read_data
  val rdataPartialLoad = LookupTreeDefault(lsuOpType, &quot;b0&quot;.U(64.W), List(
      LSUOpType.lb   -&gt; SignExt(rdataSel(7, 0) , 64),
      LSUOpType.lh   -&gt; SignExt(rdataSel(15, 0), 64),
      LSUOpType.lw   -&gt; SignExt(rdataSel(31, 0), 64),
      LSUOpType.ld   -&gt; SignExt(rdataSel(63, 0), 64),
      LSUOpType.lbu  -&gt; ZeroExt(rdataSel(7, 0) , 64),
      LSUOpType.lhu  -&gt; ZeroExt(rdataSel(15, 0), 64),
      LSUOpType.lwu  -&gt; ZeroExt(rdataSel(31, 0), 64)
  ))
  io.read_data := rdataPartialLoad
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../../gem5/custom_disk_img.html" class="navigation navigation-prev " aria-label="Previous page: 自定义磁盘镜像">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="2五级流水.html" class="navigation navigation-next " aria-label="Next page: 顺序五级流水CPU设计">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"单周期CPU实现","level":"5.1","depth":1,"next":{"title":"顺序五级流水CPU设计","level":"5.2","depth":1,"path":"ysyx/FangCore/2五级流水.md","ref":"ysyx/FangCore/2五级流水.md","articles":[]},"previous":{"title":"自定义磁盘镜像","level":"4.2","depth":1,"path":"gem5/custom_disk_img.md","ref":"gem5/custom_disk_img.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"ysyx/FangCore/1单周期CPU.md","mtime":"2022-08-18T12:19:17.402Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2023-04-12T09:28:35.525Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

